******SET YOUR DIRECTORY to ...\replication



clear all
set more off

set matsize 2000

grstyle init
grstyle set plain
grstyle set color s2

use "finaldata\final_cc_data.dta", clear

drop if fipsplace_00==.
xtset fipsplace_00 year


***Merge with long run racial segregation data
merge 1:1 fipsplace_00 year using "finaldata\LR_shblack.dta", update

drop _merge

***Merge with long run income segregation data
merge 1:1 fipsplace_00 year using "finaldata\LR_income.dta", update

keep if year==1960 | year==1970 | year==1980 | year==1990 | year==2000 | year==2010

g incomeCC_incomeMSA = (cincome / income)*100

set matsize  700
drop if fipsplace_00==.
xtset fipsplace_00 year

gen vc_pc=totnpcc_cc_offenses_vc*1000
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
g bad_soil = (1- good_soil)

local i = 1
local j = 1
g clusterDY = .
g clusterCY = .
forval yy = 1970(10)2014 {
	g YEAR`yy' = (year==`yy')
	g badsoilXY`yy' = (year==`yy') * bad_soil
	forval cc = 2/9 {
		replace clusterDY = `i' if year==`yy' & cdivcode==`cc'
		g DIV`cc'YEAR`yy' = (year==`yy' & cdivcode==`cc')
		local i = `i' + 1
	}
		forval cc = 2/4 {
		replace clusterCY = `j' if year==`yy' & cregcode==`cc'
		g REG`cc'YEAR`yy' = (year==`yy' & cregcode==`cc')
		local j = `j' + 1
	}
}
*

reghdfe cpctblk  badsoilXY* , a(fipsplace_00 i.cdivcode#i.year i.year)  cluster(cdivcode#year)

mat treat = J(6,7,1)

mat treat[1,1] = 1960
mat treat[1,2] = 0
mat treat[1,3] = 0
mat treat[1,4] = 0
mat treat[1,5] = 0
mat treat[1,6] = 0
mat treat[1,7] = 0


forval yy = 1970(10)2010 {
local pos = (`yy' - 1950)/10
mat treat[`pos',1] = `yy'
mat treat[`pos',2] = _b[badsoilXY`yy']
mat treat[`pos',3] = _b[badsoilXY`yy'] + _se[badsoilXY`yy']*invttail(e(N),0.05)
mat treat[`pos',4] = _b[badsoilXY`yy'] - _se[badsoilXY`yy']*invttail(e(N),0.05)
}

g yy = treat[_n,1] in 1/6
g eff_bl = treat[_n,2] in 1/6
g eff_bl_10 = treat[_n,3] in 1/6
g eff_bl_90 = treat[_n,4] in 1/6

label var eff_bl "Coefficient"
label var eff_bl_10 "Lower bound CI 10"
label var eff_bl_90 "Higher bound CI 90"
label var tetra_corr "Lead"

sort year

reghdfe incomeCC_incomeMSA  badsoilXY* , a(fipsplace_00 i.cdivcode#i.year i.year)  cluster(cdivcode#year)




forval yy = 1970(10)2010 {
	local pos = (`yy' - 1950)/10
	mat treat[`pos',5] = _b[badsoilXY`yy']
	mat treat[`pos',6] = _b[badsoilXY`yy'] + _se[badsoilXY`yy']*invttail(e(N),0.05)
	mat treat[`pos',7] = _b[badsoilXY`yy'] - _se[badsoilXY`yy']*invttail(e(N),0.05)
}

sort yy

g eff_inc = treat[_n,5] in 1/6
g eff_inc_10 = treat[_n,6] in 1/6
g eff_inc_90 = treat[_n,7] in 1/6



drop if year<1960

twoway (rcap eff_inc_10 eff_inc_90 yy, lc(gs10)) (scatter eff_inc yy,ms(D) mc(gs3)) (rcap eff_bl_10 eff_bl_90 yy, lc(gs10) yaxis(1) ytitle("Effect High LB"))   (scatter eff_bl yy, mc(gs3) ylabel(-8(2)4) yaxis(1) xlabel(1960(10)2010) xtitle("Year")  title("Effect of High LB on segregation") ), yline(0,lc(black) lp(-)) legend(order(2 4) label(2 "Income CC / Income MSA") label(4 "Sh. Black"))

